package com.spoorle.perf.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import com.spoorle.perf.iface.Probe;
import com.spoorle.perf.iface.Statistics;
import com.spoorle.perf.impl.ProbeFactory;

public class DefaultProbeImplTest {

	@Test
	public void testGetProbe() {
		Probe probe = ProbeFactory.getProbe(this.getClass());
		assertNotNull(probe);
	}
	
	@Test
	public void testGetStatsOnce() {
		Probe probe = ProbeFactory.getProbe(this.getClass());
		probe.enable();
		probe.start("My test operation");
		probe.stop("My test operation");
		Statistics stats = probe.getStatistics("My test operation", this.getClass());
		
		assertNotNull(stats);
	}
	
	@Test
	public void testGetStatsTwice() {
		Probe probe = ProbeFactory.getProbe(this.getClass());
		probe.enable();
		probe.start("My test operation");
		probe.stop("My test operation");
		Statistics stats = probe.getStatistics("My test operation", this.getClass()); 
		assertNotNull(stats);
		probe.start("My test operation");
		try {
			Thread.currentThread().sleep(10);
		} catch (InterruptedException e) {			
			e.printStackTrace();
		}
		probe.stop("My test operation");
		stats = probe.getStatistics("My test operation", this.getClass()); 
		assertNotNull(stats);
		assertTrue(stats.getCount()>1);
	}
	
	@Test
	public void testGetStatsOtherThread() {
		Probe probe = ProbeFactory.getProbe(this.getClass());
		probe.enable();
		probe.start("My test operation");
		probe.stop("My test operation");
		Statistics stats = probe.getStatistics("My test operation", this.getClass());
		long cnt = stats.getCount();
		assertNotNull(stats);
		probe.start("My test operation");
		Thread otherThread = new Thread() {
			@Override
			public void run() {
				Probe probe = ProbeFactory.getProbe(DefaultProbeImplTest.class);
				probe.start("My test operation");
				probe.stop("My test operation");
			}		
		};
		try {
			otherThread.start();
			Thread.currentThread().sleep(10);
		} catch (InterruptedException e) {			
			e.printStackTrace();
		}
		stats = probe.getStatistics("My test operation", this.getClass());
		probe.stop("My test operation");
		stats = probe.getStatistics("My test operation", this.getClass()); 
		assertNotNull(stats);
		assertTrue(stats.getCount()==cnt+2);
	}
	

	@Test
	public void testRelease() {
		Probe probe = ProbeFactory.getProbe(this.getClass());
		probe.enable();
		probe.start("Other op");
		probe.stop("Other op");
		probe.release();
		assertTrue(probe.getStatistics()!=null&&probe.getStatistics().size()==0);
		assertTrue(probe.getStatistics()!=null&&!probe.isEnabled());
	}
	
	@Test
	public void testSpool() {
		Probe probe = ProbeFactory.getProbe(this.getClass());
		probe.enable();
		probe.start("Other op");
		probe.stop("Other op");
		probe.start("second");
		probe.stop("second");
		
		probe.start("third");
		probe.stop("third");
		Probe probe2 = ProbeFactory.getProbe(String.class);
		probe2.start("str");
		probe2.stop("str");
		probe.start("Other op");
		probe.stop("Other op");
		String spool = probe.spool();
		System.out.println(spool);
		assertTrue(spool!=null&&spool.length()>0);
	}
	


}
